package cn.bugstack.xfg.dev.tech.test;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.ai.document.Document;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.PgVectorStore;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.PathResource;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class JGitTest {

    @Resource
    private OllamaChatClient ollamaChatClient;
    @Resource
    private TokenTextSplitter tokenTextSplitter;
    @Resource
    private SimpleVectorStore simpleVectorStore;
    @Resource
    private PgVectorStore pgVectorStore;

    @Test
    public void test() throws Exception {
        // 这部分替换为你的
        String repoURL = "https://github.com/oeljeklaus-you/UserActionAnalyzePlatform.git";
//        String repoURL = "https://gitcode.net/blueblueskyhua/360-sneakers-viewer.git";
        String username = "@linuxjavac6";
        String password = "DqUc-xu2vxvqsLkGWzaSagzvE-593";

        String localPath = "./cloned-repo";
        log.info("克隆路径：" + new File(localPath).getAbsolutePath());

        FileUtils.deleteDirectory(new File(localPath));

        Git git = Git.cloneRepository()
                .setURI(repoURL)
                .setDirectory(new File(localPath))
                .setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password))
                .call();

        git.close();
    }

    @Test
    public void test_file() throws IOException {
        Files.walkFileTree(Paths.get("./cloned-repo"), new SimpleFileVisitor<>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                try {
                    // 过滤非文本文件（可根据需要扩展）
                    if (!isSupportedFileType(file)) {
                        return FileVisitResult.CONTINUE;
                    }

                    log.info("处理文件: {}", file);
                    PathResource resource = new PathResource(file);
                    TikaDocumentReader reader = new TikaDocumentReader(resource);

                    List<Document> documents = reader.get();
                    if (documents == null || documents.isEmpty()) {
                        log.warn("文件内容为空: {}", file);
                        return FileVisitResult.CONTINUE;
                    }

                    // 处理文档内容
                    List<Document> documentSplitterList = tokenTextSplitter.apply(documents);
                    documents.forEach(doc -> doc.getMetadata().put("knowledge", "group-buy-market-liergou"));
                    documentSplitterList.forEach(doc -> doc.getMetadata().put("knowledge", "group-buy-market-liergou"));
                    pgVectorStore.accept(documentSplitterList);

                } catch (IllegalArgumentException e) {
                    log.error("文件解析失败: {}", file, e);
                }
                return FileVisitResult.CONTINUE;
            }

            private boolean isSupportedFileType(Path file) {
                String fileName = file.getFileName().toString().toLowerCase();
                return fileName.endsWith(".txt") || fileName.endsWith(".md") || fileName.endsWith(".java");
            }
        });
    }
}

